package com.latvia.ui;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.latvia.cookingapp.R;
import com.latvia.managers.SaveForPicture;

public class TakePictureActivity extends Activity {

	ImageView iv;
	boolean isImageTaken = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_take_picture);
		
		ActionBar actionBar = getActionBar();
		actionBar.setTitle("Take a Picture");
		
		
		iv = (ImageView) findViewById(R.id.takePictureImage);
		if (savedInstanceState != null){
			isImageTaken = savedInstanceState.getBoolean("keyIsImageTaken");
			if (isImageTaken){
				Bitmap imageBitmap = savedInstanceState.getParcelable("keyTakenPicture");
				if (imageBitmap != null){
					iv.setImageBitmap(imageBitmap);
				}
			}
		}
		//Bottom to take picture
		Button btn = (Button) findViewById(R.id.takePictureButton);
		btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//I think that this does the picture activity
				Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
				//This activity will be started when picture will be taken
				startActivityForResult(intent, 0);
			}
		});
		
		//bottom to save picture
		Button btn2 = (Button) findViewById(R.id.saveToGalleryButton);
		btn2.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//gets the bitmap from ImageView
				if (isImageTaken){
					Bitmap imageBitmap = ((BitmapDrawable)iv.getDrawable()).getBitmap();
					SaveForPicture saveForPicture = new SaveForPicture();
					saveForPicture.SaveImage(getBaseContext(), imageBitmap);
				} else {
					Toast toast = Toast.makeText(getApplicationContext(), "No image captured!", Toast.LENGTH_SHORT);
					toast.show();
				}
			}
		});
		
		Button btn3 = (Button) findViewById(R.id.cancelButton);
		btn3.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(TakePictureActivity.this, MainActivity.class );
				intent.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP );
				TakePictureActivity.this.startActivity( intent );
			}
		});
	}
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data){
		if (requestCode == 0 && data != null){
				Bitmap theImage = (Bitmap) data.getExtras().get("data");
				iv.setImageBitmap(theImage);
				isImageTaken = true;
		}
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.take_picture, menu);
		return true;
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
	    super.onSaveInstanceState(outState);
	    if (isImageTaken){
	    	Bitmap imageBitmap = ((BitmapDrawable)iv.getDrawable()).getBitmap();
		    if (imageBitmap != null){
		    	outState.putParcelable("keyTakenPicture", imageBitmap);
		    	outState.putBoolean("keyIsImageTaken", isImageTaken);
		    }
	    }
	}

}
